class Find_Path
  def zdsq_mapdam(x,y)
    px = x
    py = y
    for i in $game_variables[72]
      xx=($game_map.events[i[0]].x-px).abs
      yy=($game_map.events[i[0]].y-py).abs
      case i[1]
      when 1 #矩形领域
        w=i[2][0]/2
        h=i[2][1]/2
        return true if xx<=w && yy<=h
      end
    end#领域怪
    for i in $game_variables[222] #沙尘暴
      xx=($game_map.events[i].x-px).abs
      yy=($game_map.events[i].y-py).abs
      na=$game_map.events[i].event.name
      ee=na[6,na.length-6].to_i
      a=got_p_value(ee,112).split("-")
      return true if xx+yy<=a[0].to_i
    end #沙尘暴
    for i in 0...$game_variables[129].size
      xx=$game_map.events[$game_variables[129][i]].x-x
      yy=$game_map.events[$game_variables[129][i]].y-y
      xxx=xx.abs
      yyy=yy.abs
      na=$game_map.events[$game_variables[129][i]].event.name
      ee=na[6,na.length-6].to_i
      if (xxx<=1 || yyy<=1) && got_p(ee,234) #游猎
        return true
      end
      if (xx==0 || yy==0)
        sw=true
        if xx==0
          if yy>0
            d=8
          else
            d=2
          end
        end
        if yy==0
          if xx>0
            d=4
          else
            d=6
          end
        end
        x2=$game_map.events[$game_variables[129][i]].x
        y2=$game_map.events[$game_variables[129][i]].y
        if x2 !=x || y2 !=y
          case d
          when 2
            y2+=1
          when 4
            x2-=1
          when 6
            x2+=1
          when 8
            y2-=1
          end
          while x2 != x || y2 != y
            xx=x2
            yy=y2
            case d
            when 2
              yy+=1
            when 4
              xx-=1
            when 6
              xx+=1
            when 8
              yy-=1
            end
            bit = (1 << (d / 2 - 1)) & 0x0f #1111
            for k in [2, 1]
              if xx==0
                tile_id = $game_map.data[x2, y2, k]
              else
                tile_id = $game_map.data[x2, y2, k]
              end
              if tile_id == nil
                sw=false
              elsif $game_map.terrain_tags[tile_id]>0
                sw=true
                break
              elsif $game_map.passages[tile_id] & bit != 0
                sw=false
              elsif $game_map.passages[tile_id] & 0x0f == 0x0f
                sw=false
              elsif $game_map.priorities[tile_id] == 0
                break
              end
            end
            bit = (1 << ( ( 10 - d ) / 2 - 1)) & 0x0f
            for k in [2, 1]
              if xx==0
                tile_id = $game_map.data[xx, yy, k]
              else
                tile_id = $game_map.data[xx, yy, k]
              end
              if $game_map.terrain_tags[tile_id]>0
                sw=true
                break
              end
              if tile_id == nil
                sw=false
              elsif $game_map.terrain_tags[tile_id]>0
                sw=true
                break
              elsif $game_map.passages[tile_id] & bit != 0
                sw=false
              elsif $game_map.passages[tile_id] & 0x0f == 0x0f
                sw=false
              elsif $game_map.priorities[tile_id] == 0
                break
              end
            end
            x2=xx
            y2=yy
            break if !sw
          end
          
        end
        if sw
          md=$game_actors[$game_variables[1]+1].dex
          shield=$game_actors[$game_variables[1]+1].prshield
          mmiss=1-$game_actors[$game_variables[1]+1].miss
          if got_p(ee,105) #致命射击
            mmiss=1
            shield=$game_actors[$game_variables[1]+1].shield
            shield=$game_actors[$game_variables[1]+1].prshield(shield*(1-got_p_value(ee,105).to_f/100))
          end
          ea=$data_enemies[ee].atk
          ecombo=$data_enemies[ee].agi
          ecombo=Math.sqrt(ecombo)/10.0
          damage=([ea-md,0].max*ecombo*shield*mmiss)
          if got_p(ee,26) #元素冲击
            damage+=got_magicdam(got_p_value(ee,26).to_f)*ecombo
          end
          savedamage=damage
          if got_p(ee,145) #强力击
            $game_variables[235]=[] if $game_variables[235]==0
            damage=0
            if $game_variables[235][$game_variables[129][i]]
              return false
            else
              if xxx==0
                l=yyy-1
              else
                l=xxx-1
              end
              a=got_p_value(ee,145).split("-")
              damage+=got_magicdam(a[1].to_f*(1+a[2].to_f/100*l))
              return true if damage>0
            end
          end
          if got_p(ee,204) #幽冥射击
            damage=0
            a=got_p_value(ee,204).split("-")
            damage+=ea*shield/100.0*a[0].to_f
            return true if damage>0
          end
          if got_p(ee,205) #诅咒之矢
            damage=0
            a=got_p_value(ee,205).split("-")
            damage+=got_magicdam(ea)/100.0*a[0].to_f
            return true if damage>0
          end
          return true if damage>0
        end
      end
    end #精准射击怪
    if $game_variables[121].size>=2
      c=[-5,-4,-3,-2]
      s=[]
      for ev in $game_variables[121]
        event=$game_map.events[ev]
        if (event.x-px).abs+(event.y-py).abs==1 && event.character_name!="" && event.name[0,6]=="怪物"
          if event.x<px
            c[0]=$jjc[[event.x,event.y]]
            s[0]=event.id
          end
          if event.x>px
            c[2]=$jjc[[event.x,event.y]]
            s[2]=event.id
          end
          if event.y<py
            c[1]=$jjc[[event.x,event.y]]
            s[1]=event.id
          end
          if event.y>py
            c[3]=$jjc[[event.x,event.y]]
            s[3]=event.id
          end
        end
      end
      i=0
      while i<=1
        if c[i]==c[i+2]
          sign=-1
          for tt in 0...$game_variables[183].size
            if $game_variables[183][$game_variables[183].size-1-tt]>0 && $game_actors[$game_variables[1]+1].all_skill.include?($game_variables[183][$game_variables[183].size-1-tt])
              sign=$game_variables[183].size-1-tt
              break
            end
          end
          if sign==-1
            checkdam=([$event_damage[s[i]],0].max+[$event_damage[s[i+2]],0].max)*0.5
            return true if checkdam>0
          else
            return true
          end
        end
        i+=1
      end
    end#夹击怪
    for i in $game_variables[280]
      ev=$game_map.events[i]
      xx=ev.x-px
      yy=ev.y-py
      if (xx==0 or yy==0)
        if xx==0
          d=yy>0 ? 8 : 2
        end
        if yy==0
          d=xx>0 ? 4 : 6
        end
        ex=ev.x
        ey=ev.y
        sw=true
        while ex != px || ey != py
          xx=ex
          yy=ey
          case d
          when 2
            yy+=1
          when 4
            xx-=1
          when 6
            xx+=1
          when 8
            yy-=1
          end
          bit = (1 << (d / 2 - 1)) & 0x0f
          for k in [2, 1]
            if xx==0
              tile_id = $game_map.data[x, y, k]
            else
              tile_id = $game_map.data[x, y, k]
            end
            if tile_id == nil
              sw=false
            elsif $game_map.terrain_tags[tile_id]>0
              sw=true
              break
            elsif $game_map.passages[tile_id] & bit != 0
              sw=false
            elsif $game_map.passages[tile_id] & 0x0f == 0x0f
              sw=false
            elsif $game_map.priorities[tile_id] == 0
              break
            end
          end
          bit = (1 << ( ( 10 - d ) / 2 - 1)) & 0x0f
          for k in [2, 1]
            if xx==0
              tile_id = $game_map.data[xx, yy, k]
            else
              tile_id = $game_map.data[xx, yy, k]
            end
            if $game_map.terrain_tags[tile_id]>0
              sw=true
              break
            end
            if tile_id == nil
              sw=false
            elsif $game_map.terrain_tags[tile_id]>0
              sw=true
              break
            elsif $game_map.passages[tile_id] & bit != 0
              sw=false
            elsif $game_map.passages[tile_id] & 0x0f == 0x0f
              sw=false
            elsif $game_map.priorities[tile_id] == 0
              break
            end
          end
          ex=xx
          ey=yy
          bolist=$game_map.check_all_event(xx, yy)
          for bob in bolist
            bobo=$game_map.events[bob]
            sw=false if !bobo.get_through
            sw=false if d==2 && $game_map.events[i].y-bobo.y==-1
            sw=false if d==4 && $game_map.events[i].x-bobo.x==1
            sw=false if d==6 && $game_map.events[i].x-bobo.x==-1
            sw=false if d==8 && $game_map.events[i].y-bobo.y==1
            break if !sw
          end
          break if !sw
        end
        return true if sw
      end
    end #追猎怪
    for i in $game_variables[119]
      xx=($game_map.events[i].x-px).abs
      yy=($game_map.events[i].y-py).abs
      return true if (xx==1 && yy<1) || (xx<1 && yy==1)
    end#阻击怪
    for i in $game_variables[223]
      xx=($game_map.events[i].x-px).abs
      yy=($game_map.events[i].y-py).abs
      return true if (xx==1 && yy<1) || (xx<1 && yy==1)
    end#捕捉怪
    return false
  end
end
